package org.dromara.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.community.entity.AsCommunityAnnouncements;
import org.dromara.community.entity.AsPartyBuilding;
import org.dromara.community.entity.dto.AsCommunityAnnouncementsDTO;
import org.dromara.community.entity.dto.AsPartyBuildingDTO;
import org.dromara.community.mapper.AsPartyBuildingMapper;
import org.dromara.community.service.AsPartyBuildingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AsPartyBuildingServiceimpl extends ServiceImpl<AsPartyBuildingMapper, AsPartyBuilding> implements AsPartyBuildingService {

    // 七牛云的accessKey
    private static String accessKey = "bia0cM3uFDJ69kx_tzT255eeyNjQ2EomQuv5nKaT";

    // 七牛云的secretKey
    private static String secretKey = "_EhxA9ZRNm-JjR7Q6IdxdQeIOziG_v9A9xKTy3Fq";

    // 七牛云的存储空间名称
    private static String bucket = "commpro";

    // 七牛云的存储空间链接
    private static String path = "http://shfflvwvr.hb-bkt.clouddn.com";

    @Autowired
    private AsPartyBuildingMapper asPartyBuildingMapper;

    @Override
    /**
     * 根据标题模糊查询社区公告信息
     * <p>
     * 该方法接收一个标题作为参数，通过模糊查询数据库中的公告信息，并将结果转换为DTO格式返回
     * 如果输入的标题为空或仅包含空格，则直接返回一个空列表
     *
     * @param title 公告标题，用于模糊查询
     * @return 返回一个包含AsCommunityAnnouncementsDTO对象的列表，该列表包含了查询到的公告信息
     */
    public List<AsCommunityAnnouncementsDTO> selectAsParty(String title) {
        // 输入参数为空时，直接返回空列表
        if (title == null || title.trim().isEmpty()) {
            return List.of();
        }

        // 创建查询包装器，用于构建查询条件
        QueryWrapper<AsPartyBuilding> queryWrapper = new QueryWrapper<>();
        // 设置模糊查询条件
        queryWrapper.like("title", title);

        // 执行查询，获取结果列表
        List<AsPartyBuilding> asPartyBuildings = asPartyBuildingMapper.selectList(queryWrapper);

        // 将查询结果转换为 DTO 类型
        return asPartyBuildings.stream()
            .map(announcement -> {
                AsCommunityAnnouncementsDTO dto = new AsCommunityAnnouncementsDTO();
                // 直接复制属性
                BeanUtils.copyProperties(announcement, dto);
                return dto;
            })
            .collect(Collectors.toList());
    }

    @Override
    /**
     * 根据图片ID查询公告信息
     *
     * @param imageId 图片ID
     * @return 返回一个包含AsPartyBuildingDTO对象的列表，每个对象代表一条公告的详细信息
     */
    public List<AsPartyBuildingDTO> selectAnnouncementByImageId(Integer imageId) {

        // 初始化一个空的ArrayList，用于存储查询到的公告信息DTO对象
        ArrayList<AsPartyBuildingDTO> asPartyBuildingDTOArrayList = new ArrayList<>();

        // 调用Mapper层的方法，根据图片ID查询公告信息，返回一个包含AsPartyBuilding对象的列表
        List<AsPartyBuildingDTO> asCommunityAnnouncements = asPartyBuildingMapper.selectAnnouncementByImageId(imageId);

        // 遍历查询到的公告信息列表
        for (AsPartyBuilding asPartyBuilding : asCommunityAnnouncements) {
            // 封装一条公告信息到DTO对象中
            AsPartyBuildingDTO asPartyBuildingDTO = new AsPartyBuildingDTO();
            // 使用BeanUtils工具类将AsPartyBuilding对象的属性复制到AsPartyBuildingDTO对象中
            BeanUtils.copyProperties(asPartyBuilding, asPartyBuildingDTO);

            // 使用逗号分割字符串，处理多图片URL的情况
            String[] urlArray = asPartyBuilding.getUrl().split(",");

            // 将分割后的URL存入List集合
            List<String> urlList = new ArrayList<>(Arrays.asList(urlArray));
            asPartyBuildingDTO.setUrlList(urlList);

            // 将封装好的DTO对象添加到列表中
            asPartyBuildingDTOArrayList.add(asPartyBuildingDTO);
        }
        // 返回包含所有公告信息DTO对象的列表
        return asPartyBuildingDTOArrayList;
    }
}
